#ifndef __DBROLE_ENTERRECIEVER_H__
#define __DBROLE_ENTERRECIEVER_H__

#include "gwmessagetab.hpp"
#include "IMessageOperator.h"
#include "protonet.hpp"
#include "../DBServerTab.h"
#include "sqlquery/QueryRoleDetail.h"
#include "objectinfo.pb.h"
#include "gatewayserver.pb.h"
#include "../Sender/DBRole_Enter_FailedSender.h"
#include "../Sender/DBRole_Enter_SuccessSender.h"

/**
*@file       DBRole_EnterReciever.h
*@brief      process message DBRole_Enter
*
*@author     graywind
*@version    1.0.0.0
*@data       2012/3/28
*/

///process message
class OnDBRole_Enter
{
public:
    ///@brief    process message
    ///param[in] op            operator type
    ///param[in] session_id    identifier of session
    ///param[in] pMsgBuffer    message buffer
    ///param[in] msg_bytes     byte length of message buffer
    ///retval    void
    static void OnMapping(IMessageOperator* op, int session_id, void* pMsgBuffer, unsigned long msg_bytes)
    {
        //TODO
        int error = EnterFailed::eErrorCode_MIN - 1;
        DBRoleEnter param;
        if(ProtoNet<DBRoleEnter>::CreateFromBytes(param, pMsgBuffer, msg_bytes))
        {
            QueryRoleDetail::stmt->setString(1, param.rolecode());
            if(QueryRoleDetail::stmt->execute())
            {
                do
                {
                    sql::ResultSet* result = QueryRoleDetail::stmt->getResultSet();
                    if(result->next())
                    {
                        int usr_id = result->getInt(1);
                        sql::SQLString usr_name = result->getString(2);
                        sql::SQLString role_code = result->getString(3);
                        sql::SQLString role_name = result->getString(4);
                        int gender = result->getInt(5);
                        int job = result->getInt(6);
                        int world_id = result->getInt(7);
                        std::istream* stream = result->getBlob(8);
                        RoleInfoDetail::DataDetail data_detail;
                        data_detail.ParseFromIstream(stream);

                        if(param.usrid() != usr_id)
                        {
                            error = EnterFailed::UsrId_Invalid;
                            break;
                        }

                        if(param.usrname() != usr_name)
                        {
                            error = EnterFailed::UsrName_Invalid;
                            break;
                        }

                        if(param.worldid() != world_id)
                        {
                            error = EnterFailed::WorldId_Invalid;
                            break;
                        }

                        msgDBRole_Enter_Success msg(session_id, param.gatecode(), usr_id, usr_name, role_code, role_name, gender, job, world_id, &data_detail);
                        gwMessagePort::ProcessMessage(&msg);
                    }
                    else
                        error = EnterFailed::RoleCode_Invalid;
                }while(0);
            }

            if(error >= EnterFailed::eErrorCode_MIN)
            {
                msgDBRole_Enter_Failed msg(session_id, param.gatecode(), (EnterFailed::eErrorCode)error);
                gwMessagePort::ProcessMessage(&msg);
            }

            QueryRoleDetail::stmt->clearParameters();
        }
    }
};

///[message group]  [message type]  [function table] [handler]
RegisterMessageTab(DBServerTypes, DBRole_Enter, DBServerTab, OnDBRole_Enter);
#endif
